package fr.inserm.um915.bdibd.model;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sleepycat.je.Transaction;

public class BuildConfig
	{
	private static final Logger LOG=Logger.getLogger("fr.inserm.um915.bdibd");
	private File pedigree=null;
	private File reskin=null;
	private File dataPath=null;
	private File mapping=null;
	private File bedMarker=null;
	private File dbHome=null;
	private File graphviz=null;
	
	public static void main(String[] args) throws Exception
		{
		LOG.setLevel(Level.ALL);
		Model m=new Model();
		Transaction txn=null;
		BuildConfig cfg=new BuildConfig();
		
		int optind=0;
		while(optind< args.length)
			{
			if(args[optind].equals("-h"))
				{
				System.err.println(" -D <db-home>");
				System.err.println(" -p <pedigree file>");
				System.err.println(" -r <reskin file>");
				System.err.println(" -t <data-path>");
				System.err.println(" -m <mapping> (or use -b)");
				System.err.println(" -b <bed> (or use -m)");
				System.err.println(" -g <graphviz>");
				return;
				}
			else if(args[optind].equals("-D") && optind+1< args.length)
				{
				cfg.dbHome=new File(args[++optind]);
				}
			else if(args[optind].equals("-p") && optind+1< args.length)
				{
				cfg.pedigree=new File(args[++optind]);
				}
			else if(args[optind].equals("-r") && optind+1< args.length)
				{
				cfg.reskin=new File(args[++optind]);
				}
			else if(args[optind].equals("-m") && optind+1< args.length)
				{
				cfg.mapping=new File(args[++optind]);
				}
			else if(args[optind].equals("-b") && optind+1< args.length)
				{
				cfg.bedMarker=new File(args[++optind]);
				}
			else if(args[optind].equals("-t") && optind+1< args.length)
				{
				cfg.dataPath=new File(args[++optind]);
				}
			else if(args[optind].equals("-g") && optind+1< args.length)
				{
				cfg.graphviz=new File(args[++optind]);
				}
			else if(args[optind].equals("--"))
				{
				++optind;
				break;
				}
			else if(args[optind].startsWith("-"))
				{
				System.err.println("Unknown option "+args[optind]);
				return ;
				}
			else
				{
				break;
				}
			++optind;
			}
		
		if(cfg.dbHome==null)
			{
			System.err.println("dbHome undefined");
			return;
			}
		
		if(cfg.pedigree==null)
			{
			System.err.println("pedigree undefined");
			return;
			}
		if(cfg.mapping==null && cfg.bedMarker==null)
			{
			System.err.println("mapping  or bed undefined");
			return;
			}
		
		if(cfg.mapping!=null && cfg.bedMarker!=null)
			{
			System.err.println("both mapping and bed defined");
			return;
			}
		
		if(cfg.dataPath==null)
			{
			System.err.println("dataPath undefined");
			return;
			}
		/*
		cfg.dbHome=new File("/BELENOS/data/project/20111031jibdstatus/_ignore.backup/data");
		cfg.reskin=new File("/BELENOS/data/project/20110922ibdbrugada/data/_ignore.backup/ibdldreskin_brugada7");
		cfg.dataPath= new File("/home/lindenb/src/bdbidb/data/ibdpath.txt");
		cfg.mapping=new File("/home/lindenb/src/bdbidb/data/Axiom_GW_Hu_SNP.r2.na30.annot.csv.gz");
		cfg.pedigree=new File("/BELENOS/data/project/20110922ibdbrugada/data/_ignore.backup/brugada7.fam");
		*/
		m.open(cfg.dbHome,false);
		m.readPedigree(txn, cfg.pedigree,cfg.graphviz);
		m.readDataPath(txn, cfg.dataPath);
		m.readReskin(txn,cfg.reskin );
		if(cfg.mapping!=null)
			{
			m.readMapping(txn,cfg.mapping);
			}
		else
			{
			m.readBed(txn,cfg.bedMarker);
			}
		m.close();
		LOG.info("Done.");
		}
	
	
	
	
	
	}
